package leetCode;

import java.util.*;

public class Q946 {
//    public boolean validateStackSequences(int[] pushed, int[] popped) {
//        Deque<Integer> stack = new ArrayDeque<>();
//        int j = 0; //索引popped
//        for (int i = 0; i < pushed.length; i++) {
//            stack.push(pushed[i]);
//            while (!stack.isEmpty() && stack.peek() == popped[j]) {//栈顶等于要出栈的元素，直接弹出，找下一个要出栈的元素
//                j++;
//                stack.pop();
//            }
//        }
//        return stack.isEmpty();
//    }

    private static Stack<String> stack1=new Stack<String>();
    private static Stack<String> stack2=new Stack<String>();
    private static List<String> list=new ArrayList<String>();

    public static void ff(String str){
        if(stack1.isEmpty()&&stack2.isEmpty()){
            list.add(str.trim());
            return;
        }

        if(!stack2.isEmpty()){
            String str1=stack2.pop();
            ff(str+" "+str1);
            stack2.push(str1);
        }
        if(!stack1.isEmpty()){
            String str2=stack1.pop();
            stack2.push(str2);
            stack2.pop();
            stack1.push(str2);
        }
    }



    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        while(scanner.hasNext()){
            int n=scanner.nextInt();
            scanner.nextLine();
            String str=scanner.nextLine();
            String[] ss=str.split(" ");
            for(int i=ss.length-1;i>=0;i--)
                stack1.push(ss[i]);
            ff("");
            for(String s:list)
                System.out.println(s);
        }
    }

}
